package com.dao;

import java.util.List;
import java.util.Map;
import com.servlet.M;
import com.servlet.M2;
import com.servlet.My;

import hyl.core.MyFun;
import hyl.core.db.MyDB;
import hyl.ext.base.ILogin;
import hyl.ext.base.MySession;

public class SqlOfUser implements ILogin {
//	public static void main(String[] args) {
//		//Random random = new Random(System.currentTimeMillis());
//		//1623825500158
//		//3371322064768539
//	       for (int i = 0; i < 2; i++) {
//	            //创建2个随机数生成器
//	            Random random = new Random();
//	            for (int j = 0; j < 3; j++) {
//	                //每个随机数生成器产生8个数
//	                //n 要返回的随机数的边界 也就是随机数的范围是0-50之间的整数
//	                System.out.println(random.nextInt(50) + ",");
//	            }
//	            System.out.println(MyFun.getNs());
//	        }
//	       System.out.println( MyFun.getNsString());
//		
//		System.out.println("通过Random产生的随机数列");
//        for (int j = 0; j < 8; j++) {
//        	Random random2 = new Random();
//    		MyFun.print(random2.nextInt());
//        }
//		
//		System.out.println("通过Math.random产生的随机数列");
//        for (int j = 0; j < 8; j++) {
//        	double d=Math.random();
//            System.out.println(d+ ","+(int)(d*80000000));
//        }
//	}
	static ILogin login=new SqlOfUser();
	public static ILogin instance() {
		return login;
	}
	
	public  void setLoginSession(MySession tk, Map<String, Object> user) {
		tk.setUserId(MyFun.obj2Int(user.get("id")));
		tk.setPhone(MyFun.obj2Str(user.get("uphone")));
		tk.setUname(MyFun.obj2Str(user.get("uname")));
		tk.setRoleid(MyFun.obj2Int(user.get("roleid")));
		tk.set别名(MyFun.obj2Str(user.get("ualias")));
		tk.setOrgid(MyFun.obj2Int(user.get("orgid")));
		tk.setState(0, '1');
		tk.setIstate(MyFun.obj2Int(user.get("istate")));
		tk.tag.put("quanxian", M2.getRole(MyFun.obj2Int(user.get("roleid"))));
		// tk._tag.put("juese", M2.getistate(tk.istate));
		tk.msg.setCode(1, M.MSG_登录成功);
		tk.start();
	}
	// 读取用户信息
	public static Map<String, Object> login(String name, String pass) {
		// 最多保留10个备份
		String sql = "select a.id,a.tjrid,a.uname,a.uphone,a.roleid,a.istate,a.ualias from user a  where a.istate&1!=0  and a.upass=? and (a.uname =? or a.uphone=?)";
		Object[] params = {  pass,name,name };
		MyDB db = My.getDB1();
		return db.queryMap(sql, params);
	}

	// sso授权登录
	public static Map<String, Object> slogin(String appid, String openid) {
		/// 92233 72036 85477 5806L
		// 最多保留10个备份
		String sql = "select a.id,a.tjrid,a.uname,a.uphone,a.roleid,a.istate,a.ualias from user a left join app_user b on a.id=b.uid where  a.istate&1!=0 and b.appid =? and b.openid=? ";
		Object[] params = { appid, openid };
		MyDB db = My.getDB1();
		return db.queryMap(sql, params);
	}

	public static String get_随机账号(String 前缀, int 后缀长度) {
		String uuid = MyFun.nvlStr(前缀, "") + MyFun.getMsDesc().substring(0, 后缀长度);
		MyDB db = My.getDB1();
		String sql = "select * from user where uname=?";
		Object[] param = { uuid };
		List<Map<String, Object>> list = db.queryList(sql, param);
		if (list.size() > 0) {
			get_随机账号(前缀, 后缀长度);
		}
		return uuid;
	}

	// 查询用户id
	public static Integer cid(Object[] obj) {
		MyDB db = My.getDB1();
		String sql = "select id from user where id=? or uname=?  ";
		Object ob = db.querySingle(sql, obj);

		return MyFun.obj2Int(ob);
	}

	// 获得app的授权url
	public static String url(String appid) {
		MyDB db = My.getDB1();
		String sql = "select t3 from app where appid=? ";
		Object[] params = { appid };
		return MyFun.obj2Str(db.querySingle(sql, params));
	}

	/**
	 * 通过第三方app进入sso注册
	 */
	public static String add_User(Map<String, String> r) {
		MyDB db = My.getDB1();
		String sql = "select * from user where uphone = ?";
		Object[] parm = { r.get("uphone") };
		List<Map<String, Object>> list = db.queryList(sql, parm);
		if (list.size() > 0) {
			return "repeat";
		}

		MyDB cdb = My.getCommitDB1();
		sql = "insert into user(uname,uphone,ualias,upass,ctime,roleid)values(?,?,?,?,now(),?)";
		Object[] params = { r.get("uphone"), r.get("uphone"), r.get("ualias"), r.get("upass"), M2.ROLE_用户 };
		long id = cdb.executeUpdate(sql, params);

		String openid = MyFun.getUUID();
		sql = "insert into user_conf(id)values(?)";
		Object[] param = { id };
		cdb.executeUpdate(sql, param);

		sql = "insert into app_user(openid,appid,uid)values(?,?,?)";
		Object[] params2 = { openid, r.get("appid"), id };
		cdb.executeUpdate(sql, params2);
		cdb.commit();

		return openid;
	}

	/**
	 * 没有通过第三方app进入注册
	 */
	public static void add_User2(Map<String, String> r) {
		MyDB db = My.getCommitDB1();
		String sql = "insert into user(uphone,ualias,upass,ctime,roleid)values(?,?,?,now(),?)";
		Object[] params = { r.get("uphone"), r.get("ualias"), r.get("upass"), M2.ROLE_用户 };
		long id = db.executeUpdate(sql, params);

		sql = "insert into user_conf(id)values(?)";
		Object[] param = { id };
		db.executeUpdate(sql, param);
		db.commit();
	}

	public static String bind_app(Map<String, String> r) {
		MyDB db = My.getDB1();
		String sql = "select * from user where uphone=? and upass=? and istate=1";
		Object[] params = { r.get("uphone"), r.get("upass") };
		Map<String, Object> map = db.queryMap(sql, params);
		if (map.get("ualias") != null) {
			sql = "select * from app_user where uid=? and appid=?";
			Object[] param = { map.get("id"), r.get("appid") };
			Map<String, Object> app = db.queryMap(sql, param);
			if (app != null && app.get("openid") != null) {
				return MyFun.obj2Str(app.get("openid"));
			} else {
				MyDB cdb = My.getCommitDB1();
				String openid = MyFun.getUUID();
				sql = "insert into app_user(openid,appid,uid)values(?,?,?)";
				Object[] params2 = { openid, r.get("appid"), map.get("id") };
				cdb.executeUpdate(sql, params2);
				cdb.commit();
				return openid;
			}
		}
		return null;
	}

}
